# -*- coding: cp936 -*-
import os,sys,shelve,urllib2,re
s=shelve.open('workmates.dat')
get=0

##BU=[]
##for i in range(len(s)):
##    mBU=s[str(i)][2]
##    if not (mBU in BU):
##        BU.append(mBU)
##for m in BU:
##    print m
    
for i in range(len(s)-1):
    print i
    if 'AT' in s[str(i)][2]:
        BU='ATC'
    elif 'QA' in s[str(i)][2]:
        BU='QA'
    elif 'OMSG' in s[str(i)][2]:
        BU='OMSG'
    elif 'MCPG' in s[str(i)][2]:
        BU='MCPG'
    elif 'CPSG' in s[str(i)][2]:
        BU='CPSG'
    else:
        BU='others'
    get+=1
    if(len(s[str(i)])<7):
        break
    srcimg=s[str(i)][6].encode('gbk')
    try:
        fd=urllib2.urlopen(srcimg)
    except ValueError:
        print 'Open URL "%s" Value Error!'%srcimg
        continue
    except urllib2.HTTPError:
        print 'Open URL "%s" HTTPError!'%srcimg
        continue
    reg=re.compile(r'.*/(.*\..{3})')
    m=reg.search(srcimg)
    if m:
        dstimg=r'matebigimg/'+BU+'/'+m.groups()[0]
        f=open(dstimg,'wb')
        f.write(fd.read())
        f.close()                
    fd.close()
s.close()

